
package privatemessanger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * This class will contain all the information about the "Private Messanger" software and its creators.
 */
public class StatisticsPanel extends JPanel implements ActionListener
{
    private LogoPanel _logoPanel;
    private UserPanel _userPanel;
    private JFrame controllingFrame; //needed for dialogs
    private MainFrame _mainFrame;

    /**
     * Setting up all the varaibles
     * @param f
     * @param mainFrame
     */
    public StatisticsPanel( JFrame f, MainFrame mainFrame)
    {
        _mainFrame = mainFrame;
        controllingFrame = f;
        _logoPanel = new LogoPanel();
        _userPanel = new UserPanel(this, _mainFrame);

        f.getContentPane().add(_logoPanel, "West");
        f.getContentPane().add(_userPanel,"East");

        _logoPanel.setBackground(Color.white);
        _userPanel.setBackground(Color.white);
    }

    public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); }

    /**
     * This class will create and lay the user side panels and components
     */
    public class UserPanel extends JPanel implements ActionListener, KeyListener
    {
        private StatisticsPanel _p;
        private MainFrame _mainFrame;

        public UserPanel(StatisticsPanel p, MainFrame mainFrame)
        {
            _mainFrame= mainFrame;
            _p = p;
            PaintComponent();
        }

        /**
         * Laying all the components in the right order
         */
        private void PaintComponent()
        {
              //Lay out everything.
            JLabel _linedown = new JLabel("this is a Line                                          Down:   ");
            _linedown.setForeground(Color.white);
            _p.add(_linedown);
            _linedown.setVisible(true);

            @SuppressWarnings("static-access")
            JTextArea _about= new JTextArea(
                    "\t  Statistics:"+_mainFrame._statisticsInfo.getName()+"\n"
                    +"==========================================\n"
                    +"Maximum Users EVER and date: "
                    +_mainFrame._statisticsInfo.getMaxClients()+"  "+_mainFrame._statisticsInfo.getMaxClientsDate()
                    +"\nTotal Users Ever: "+_mainFrame._statisticsInfo.getTotalClients()
                    +"\nTotal Messages Sent: "+_mainFrame._statisticsInfo.getTotalMessages());
            
            _about.setLineWrap(true);
            _about.setWrapStyleWord(true);
            _about.setBounds(50,50, 300,50);
            _p.setLocation(100, 50);
            _p.add(_about);
            _about.setVisible(true);
            _p.setBackground(Color.white);

            JLabel _linedown1 = new JLabel("this is a Line                                          Down:   ");
            _linedown1.setForeground(Color.white);
            _p.add(_linedown1);
            _linedown1.setVisible(true);

            JComponent buttonPane = createButtonPanel();
            _p.add(buttonPane);
            _p.repaint();
        }

        /**
         * Setting up the button panel.
         * @return
         */
        protected JComponent createButtonPanel()
        {
            JPanel p = new JPanel(new GridLayout(0,2));
            JButton OKButton = new JButton("OK");

            OKButton.setActionCommand("OK");
            OKButton.addActionListener(this);
            _mainFrame.getRootPane().setDefaultButton(OKButton);
            p.setBackground(Color.white);
            p.add(OKButton);
            return p;
        }

        /**
         * If the user pressed on the buttons then do the logic (check the input) and do your action.
         * @param e
         */
        public void actionPerformed(ActionEvent e)
        {
            //The user has pressed on OK.
            controllingFrame.dispose();
        }

        /**
         * If the user pressed "ENTER" and not the buttons, do the same as if the user pressed on confirm
         * @param e
         */
        public void keyTyped(KeyEvent e)
        {
            if(e.getKeyCode()== KeyEvent.VK_ENTER)
            {
                    //The user has pressed on OK.
                    controllingFrame.dispose();
            }
        }

        public void keyPressed(KeyEvent e) { keyTyped(e); }

        public void keyReleased(KeyEvent e) { }
    }

    /**
     * Setting up the logo side of the frame.
     */
    public class LogoPanel extends JPanel
    {
        private JLabel _logo;
        private JFrame _f;

        public LogoPanel()
        {
            setBackground(Color.white);
            ImageIcon _icon = createImageIcon("logo.jpg", "About Messenger");
            _logo = new JLabel(_icon);
            add(_logo);

            _logo.setVisible(true);
            repaint();
        }

        /**
         * Create the logo image and lay it down.
         * @param path
         * @param description
         * @return
         */
        protected ImageIcon createImageIcon(String path, String description)
        {
            java.net.URL imgURL = LogoPanel.class.getResource(path);
            if (imgURL != null)
            {
                return new ImageIcon(imgURL, description);
            }
            else
            {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    }
}
